*Settings----------------------------------
#delimit ;
set scheme plotplain;

*Parameters--------------------------------------------------------------------;
local exch=2288; //Exchange rate TSH per USD
local int=50; //Interval steps for demand curve in 1000TSh
local p_max=1000; //Price range on x-axis
local No=145; //Total number of owners
local confidence	90;
local trim_tail=3; //Trim the right tail of the elasticity graphs by dropping range of final n obs
local clustervar_bdm 	shina_plot_id; //If shina_plot_id == no clustering
local clustervar_lprd 	shina_plot_id; //If shina_leader_plot_id == no clustering 
local lprd_cnt_lab="Control";
local lprd_stks_lab="Stakes"; 
local lprd_inct_lab="Incentives";
local lprd_cnt_id=0;
local lprd_stks_id=2; 
local lprd_inct_id=1;

local check_matching_shinacheck=0; //Set to 1 in order to drop the shina that are missing at least 1 treatment group (i.e. 2,6, and 14)
    *check_matching_shinacheck if shina overlap matters;

*Create "base" datasets for demand graphs--------------------------------------;
clear;
local obs=1+`p_max'/`int';
set obs `obs'; //One observation for each interval from 0 to p_max
gen float price_1000tsh = `int'*(_n-1);
sort price_1000tsh;
lab var price_1000tsh "Price (1,000TSh)";
gen uptake_bdm		= .;
lab var uptake_bdm "Share Uptake (BDM)";
gen uptake_bdm_ub	= .;
gen uptake_bdm_lb	= .;
gen revenue_bdm		= .;
lab var revenue_bdm "Total revenue (1,000TSh)";
gen uptake_lprd		= .;
lab var uptake_lprd "Share Uptake (Leader Predict)";
gen uptake_lprd_ub	= .;
gen uptake_lprd_lb	= .;
foreach t in "cnt" "stks" "inct"
	{;
	gen uptake_lprd_`t'		= .;
	lab var uptake_lprd_`t' "Share Uptake (`lprd_`t'_lab' Leader Predict)";
	gen uptake_lprd_`t'_ub	= .;
	gen uptake_lprd_`t'_lb	= .;
	};
save "temp/uptake-estimates.dta", replace;


*Create estimating datasets from BDM-------------------------------------------;
use "${sdir}/Owners_cln.dta", clear;

keep shina_id plot_id shina_plot_id s9_q_1;

local i=1;
forvalues P = 0(`int')`p_max' 
	{;
	di "gen bdm_buy_P`i'";
	gen bdm_buy_P`i' = (s9_q_1>=`P'*1000) if missing(s9_q_1)==0;
	label var bdm_buy_P`i' "Will buy CRO at `P' Thousand TSh (BDM)";
	local i=`i'+1;
	};

if `check_matching_shinacheck'==1{;
	drop if shina_id==14 | shina_id==6 | shina_id==2;
};

save "temp/demand-estimating-BDM.dta", replace;


*Create estimating datasets from Leader Prediction-----------------------------;
use "${sdir}/Leaders_and_Owners_cln.dta", clear;
keep if owner_observed==1; //Keep for same individuals as BDM
keep shina_leader_plot_id shina_leader_id shina_plot_id leader_id shina_id plot_id q42_ldr trt_grp;

*All treatments together;
local i=1;
forvalues P = 0(`int')`p_max' 
	{;
	di "gen `t'lprd_buy_P`i'";
	gen lprd_buy_P`i' = (q42_ldr>=`P'*1000) if missing(q42_ldr)==0;
	label var lprd_buy_P`i' "Will buy CRO at `P' Thousand TSh (Leader)";
	local i=`i'+1;
	};
	
*Each treatment separately;
foreach t in "cnt" "stks" "inct"
	{;
	local i=1;
	forvalues P = 0(`int')`p_max' 
		{;
		di "gen lprd_`t'_buy_P`i'";
		gen lprd_`t'_buy_P`i' = (q42_ldr>=`P'*1000) if missing(q42_ldr)==0 & trt_grp==`lprd_`t'_id';
		label var lprd_`t'_buy_P`i' "Will buy CRO at `P' Thousand TSh (`lprd_`t'_'Leader)";
		local i=`i'+1;
		};
	};
	
bysort shina_id plot_id: gen leader_wt=_N;
bysort shina_id plot_id trt_grp_ldr: gen leader_grp_wt=_N;


if `check_matching_shinacheck'==1{;
	drop if shina_id==14 | shina_id==6 | shina_id==2;
};

save "temp/demand-estimating-LPRD.dta", replace;

*Estimate uptake for both BDM and Leader Prediciton----------------------------;
*Note: we start the estimating prices at 1*`int' since the outcome does not vary at price=0;

local i = 1;
forvalues P = 0(`int')`p_max' 
	{;
	di "BDM, PRICE `P' Thousand TSh";
	use "temp/demand-estimating-BDM.dta", clear;
	
	quietly	
		{;
		sum bdm_buy_P`i';
		if r(mean)!=1 & r(mean)!=0
			{;
			logit bdm_buy_P`i', vce(cluster `clustervar_bdm');
			margins, post level(`confidence');
			
			local b=el(r(table),1,1);
			local ll=el(r(table),5,1);
			local ul=el(r(table),6,1);
			
			use "temp/uptake-estimates.dta", clear;
			replace uptake_bdm		= `b'			if price_1000tsh==`P';
			replace uptake_bdm_ub	= `ul' 			if price_1000tsh==`P';
			replace uptake_bdm_lb	= `ll'			if price_1000tsh==`P';
			replace revenue_bdm		= `b'*`P'*`No' 	if price_1000tsh==`P';
			save "temp/uptake-estimates.dta", replace;
			};
		};
			
	*All treatments together;
	di "LPRD, PRICE `P' Thousand TSh";
	use "temp/demand-estimating-LPRD.dta", clear;
	quietly	
		{;
		sum lprd_buy_P`i';
		if r(mean)!=1 & r(mean)!=0
			{;
			logit lprd_buy_P`i' [fweight=leader_wt], vce(cluster `clustervar_lprd');
			margins, post level(`confidence');
			
			local b=el(r(table),1,1);
			local ll=el(r(table),5,1);
			local ul=el(r(table),6,1);
			
			use "temp/uptake-estimates.dta", clear;
			replace uptake_lprd		= `b'		if price_1000tsh==`P';
			replace uptake_lprd_ub	= `ul' 		if price_1000tsh==`P';
			replace uptake_lprd_lb	= `ll'		if price_1000tsh==`P';
			save "temp/uptake-estimates.dta", replace;
			};
		};
			
	*For each treatment;
	foreach t in "cnt" "stks" "inct"
		{;
		
		di "LPRD `lprd_`t'_lab', PRICE `P' Thousand TSh";
		use "temp/demand-estimating-LPRD.dta", clear;
	
		quietly	
			{;
			sum lprd_`t'_buy_P`i';
			if r(mean)!=1 & r(mean)!=0
				{;
				logit lprd_`t'_buy_P`i' [fweight=leader_grp_wt], vce(cluster `clustervar_lprd');
				margins, post level(`confidence');
				
				local b=el(r(table),1,1);
				local ll=el(r(table),5,1);
				local ul=el(r(table),6,1);
				
				use "temp/uptake-estimates.dta", clear;
				replace uptake_lprd_`t'		= `b'		if price_1000tsh==`P';
				replace uptake_lprd_`t'_ub	= `ul' 		if price_1000tsh==`P';
				replace uptake_lprd_`t'_lb	= `ll'		if price_1000tsh==`P';
				save "temp/uptake-estimates.dta", replace;
				};
			};
		};
	local i=`i'+1;
	};



*===== Demand Curve Graphs ====================================================;
use "temp/uptake-estimates.dta", clear;
gen price_usd=price_1000tsh*1000/`exch'; //convert to USD

*BDM alone;
twoway 	(line uptake_bdm price_usd, 
				sort
				lcolor(navy) 
				lpattern(solid) lwidth(medium)
				) 
		(scatter uptake_bdm price_usd, 
				sort
				color(navy)
				msymbol(o)
				) 
		(rarea uptake_bdm_ub uptake_bdm_lb price_usd, 
				sort
				color(black%25) lwidth(0)
				), 
		ytitle("Share that would uptake", size(large)) ylabel(,angle(horizontal) format(%03.1f) labsize(medlarge))
		xtitle("Price ($)", size(large)) xlabel(, labsize(medlarge))
		legend(
			order(1 3) label(1 "Owner WTP") label(3 "90% CI") size(medlarge)
			ring(0) position(1) bmargin(large) region(lwidth(vthin) lcolor(black)));
graph export "figures/BDM_demand.png", replace;


*BDM and LPRD (by treatment group);
foreach t in cnt stks inct{;
	if "`t'"=="cnt"{;
		local tit "Control";};
	if "`t'"=="stks"{;
		local tit "Stakes";};
	if "`t'"=="inct"{;
		local tit "Incentives";};
	twoway 	(line uptake_bdm price_usd, 
					sort
					lcolor(navy) 
					lpattern(solid) lwidth(medium)
					)
			(scatter uptake_bdm price_usd, 
					sort
					color(navy)
					msymbol(o)
					) 
			(rarea uptake_bdm_ub uptake_bdm_lb price_usd, 
					sort
					color(black%25) lwidth(0))
					
			(line uptake_lprd_`t' price_usd, 
					sort
					lcolor(cranberry) 
					lpattern(dash) lwidth(medium)
					)
			(scatter uptake_lprd_`t' price_usd, 
					sort
					color(cranberry)
					msymbol(o)
					) 
			(rarea uptake_lprd_`t'_ub uptake_lprd_`t'_lb price_usd, 
					sort
					color(black%25) lwidth(0)),
			ytitle("Share that would uptake", size(large)) ylabel(,angle(horizontal) format(%03.1f) labsize(medlarge))
			xtitle("Price ($)", size(large)) xlabel(, labsize(medlarge))
			legend(
				order(1 4 3) label(1 "Owner WTP") label(4 "Leader (`tit')") label(3 "90% CI") size(medlarge)
				ring(0) position(1) bmargin(large) region(lwidth(vthin) lcolor(black)));
	graph export "figures/BDM_LPRD_`t'_demand.png", replace;
	};



*End------;
clear;





